package com.hxalex.simulationhandle.ui.home;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.databinding.ActivityMainBinding;
import com.hxalex.simulationhandle.ui.base.BaseActivity;
import com.hxalex.simulationhandle.ui.home.adpter.TaskAdpter;
import com.hxalex.simulationhandle.ui.home.presenter.TaskPersenter;
import com.hxalex.simulationhandle.ui.permission.CheckPermissionActivity;
import com.hxalex.simulationhandle.ui.permission.manger.PermissionManger;
import com.hxalex.simulationhandle.ui.task.AddTaskActivity;
import com.hxalex.simulationhandle.util.AppAvilbleUtils;
import com.hxalex.simulationhandle.widget.TaskPopupWindow;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetChangedListener,PermissionManger.onPermissionListener {
    private ActivityMainBinding mBinding;
    private TaskPersenter mPersenter;
    private PermissionManger permissionManger;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        initView();
      
    }


    private void initView() {
        mBinding.appbarLayout.addOnOffsetChangedListener(this);
        mPersenter = new TaskPersenter(this);
        permissionManger = PermissionManger.getManger(this);
        TaskAdpter taskListAdater = mPersenter.getTaskListAdater();
        mBinding.recyclerTaskList.setAdapter(taskListAdater);
        mBinding.recyclerTaskList.setLayoutManager(new LinearLayoutManager(this));
        mBinding.btnAdd.setOnClickListener(v ->
        {
            Intent i = new Intent(this, AddTaskActivity.class);
            startActivity(i);

        });
        mBinding.btnPermissionSet.setOnClickListener(v ->
        {
            Intent i = new Intent(this, CheckPermissionActivity.class);
            startActivity(i);
        });
        permissionManger.setOnPermissionListener(this);
        checkAllPermission();
    }


    @Override
    protected void onResume() {
        super.onResume();
        mPersenter.freshTaskList((TaskAdpter) mBinding.recyclerTaskList.getAdapter());
        checkAllPermission();
        if (AppAvilbleUtils.isAvilible(this, getResources().getString(R.string.wifi_app_package_name))) {
            mBinding.txtWifiCheck.setText("WiFi万能钥匙:已下载");
        } else {
            mBinding.txtWifiCheck.setText("WiFi万能钥匙:未下载");
        }

        if (AppAvilbleUtils.isAvilible(this, getResources().getString(R.string.browser_app_package_name))) {
            mBinding.txtBrowerCheck.setText("欧朋浏览器:已下载");
        } else {
            mBinding.txtBrowerCheck.setText("欧朋浏览器:未下载");
        }
    }


    private void checkAllPermission() {

        if (permissionManger.isHaveAllPermission) {
            mBinding.txtPermissionCheck.setText("权限:已授权");
        } else {
            mBinding.txtPermissionCheck.setText("权限:存在权限暂未授权");
        }


    }

    /***
     * 滑动toolbar效果
     * @param appBarLayout
     * @param verticalOffset
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset != 0) {
            Float offsetRate = (float) Math.abs(verticalOffset) / appBarLayout.getTotalScrollRange() * 255;
            BigDecimal offset = new BigDecimal(offsetRate).setScale(2, BigDecimal.ROUND_HALF_UP);
            BigDecimal offset2 = new BigDecimal(offsetRate / 255).setScale(2, BigDecimal.ROUND_HALF_UP);
//            mBinding.toolbar.getBackground().setAlpha((int) offset.floatValue());
            mBinding.txtTitle.setAlpha(offset2.floatValue());
        } else if (verticalOffset == 0) {
            mBinding.txtTitle.setAlpha(0);
        }
    }

    /***
     * 事件来源->TaskPopupWindow下showAtLocation
     * @param msg
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventPopupWindow(String msg) {
        if (msg.equals(TaskPopupWindow.EVENT_SHOW_WINDOW)) {
            mBinding.btnAdd.hide();
        } else if (msg.equals(TaskPopupWindow.EVENT_DISSMISS_WINDOW)) {
            mBinding.btnAdd.show();
        }
    }

    @Override
    public void onPermissionCheck() {
        checkAllPermission();
    }
    
}
